import 'package:sqflite/sqflite.dart';
import '../base/model_interface/AbModelInterface.dart';
import '../bean/CabinetLocation.dart';
import '../bean/FirstLevelCategory.dart';
import '../bean/TwoLevelCategory.dart';
import '../bean/UserInfo.dart';

// 数据库数据
class DatabaseHelper extends AbModelInterface {
  static final DatabaseHelper _databaseHelper = DatabaseHelper._();

  DatabaseHelper._();

  String firstLevelCategoryTableName = "first_level_category";
  String articlesTableName = "articles";
  String twoLevelCategoryTableName = "two_level_category";
  String userTableName = "user";
  late Database _db;

//初始化数据库
  Future<Database> init(String pathS) async {
    _db = await openDatabase(pathS, version: 1,
        onCreate: (Database db, int version) async {
      var firstLevelCategory =
          "CREATE TABLE first_level_category (Name TEXT NOT NULL,ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Ctime TEXT DEFAULT (0) NOT NULL, Utime TEXT DEFAULT (0) NOT NULL, SortNum INTEGER DEFAULT (0) NOT NULL, Uid INTEGER DEFAULT (0) NOT NULL, Amount INTEGER DEFAULT (0) NOT NULL)";
      var twoLevelCategory =
          "CREATE TABLE two_level_category (Name TEXT NOT NULL,ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Ctime TEXT DEFAULT (0) NOT NULL, Utime TEXT DEFAULT (0) NOT NULL, SortNum INTEGER DEFAULT (0) NOT NULL, Uid INTEGER DEFAULT (0) NOT NULL,  Amount INTEGER DEFAULT (0) NOT NULL, oid INTEGER DEFAULT (0) NOT NULL);";
      var articles =
          "CREATE TABLE articles (Name TEXT NOT NULL,ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Ctime TEXT DEFAULT (0) NOT NULL, Utime TEXT DEFAULT (0) NOT NULL, SortNum INTEGER DEFAULT (0) NOT NULL, Uid INTEGER DEFAULT (0) NOT NULL,  Amount INTEGER DEFAULT (0) NOT NULL, oid INTEGER DEFAULT (0) NOT NULL, Tid INTEGER DEFAULT (0) NOT NULL, Lid INTEGER DEFAULT (0) NOT NULL, BuyTime TEXT, imgUrl TEXT NOT NULL, 'values' INTEGER, tag TEXT, 'size' TEXT NOT NULL, fabric TEXT, brand TEXT, season TEXT, color TEXT, remark TEXT)";
      var user =
          "CREATE TABLE user (Name TEXT NOT NULL,ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, pwd TEXT NOT NULL, token TEXT NOT NULL)";

      // 添加初始化数据
      await db.execute(firstLevelCategory);
      await db.execute(twoLevelCategory);

      // await db.execute(articles);
      // await db.execute(user);
    });
    await insertTestOne();
    await insertTestTwo();
    return _db;
  }

  factory DatabaseHelper() {
    return _databaseHelper;
  }

  @override
  Future<List<FirstLevelCategory>> getALLOneCategory() async {
    List<FirstLevelCategory> list = [];
    var query = _db.query(firstLevelCategoryTableName);
    // var query = db.rawQuery("select * from first_level_category ");
    await query.then((value) => {
          for (var element in value)
            {list.add(FirstLevelCategory.fromJson(element))}
        });
    return list;
  }

  @override
  Future<FirstLevelCategory> insertOneCategory(
      FirstLevelCategory firstLevenlCategory) async {
    firstLevenlCategory.id = await _db.insert(
        firstLevelCategoryTableName, firstLevenlCategory.toJson());
    return firstLevenlCategory;
  }

  @override
  Future<List<TwoLevelCategory>> getALLTwoCategory(int id) async {
    List<TwoLevelCategory> list = [];
    var query =
        _db.query(twoLevelCategoryTableName, where: 'oid = ?', whereArgs: [id]);
    // var query = db.rawQuery("select * from first_level_category ");
    await query.then((value) => {
          for (var element in value)

            {list.add(TwoLevelCategory.fromJson(element))}
        });
    return list;
  }

  @override
  Future<TwoLevelCategory> insertTwoCategory(
      TwoLevelCategory twoLevelCategory) async {
    twoLevelCategory.id =
        await _db.insert(twoLevelCategoryTableName, twoLevelCategory.toJson());
    return twoLevelCategory;
  }

  /// 登录
  Future<UserInfo> login(UserInfo userInfo) async {
    userInfo.id = await _db.insert(userTableName, userInfo.toJson());
    return userInfo;
  }

  /// 退出登录
// Future<FirstLevelCategory> logout(String token) async {
//   firstLevenlCategory.id = await _db.insert(
//       firstLevelCategoryTableName, firstLevenlCategory.toJson());
//   return firstLevenlCategory;
// }

  ///用户信息更新
// Future<FirstLevelCategory> userInfoUpdata(String token) async {
//   firstLevenlCategory.id = await _db.insert(
//       firstLevelCategoryTableName, firstLevenlCategory.toJson());
//   return firstLevenlCategory;
// }

  var insertCabinetLocationList = [
    CabinetLocation(name: '主卧衣柜'),
    CabinetLocation(name: '侧卧衣柜'),
  ];
  var insertTestFirstLevelCategoryList = [
    FirstLevelCategory(name: '无分类', id: 1),
    FirstLevelCategory(name: '内衣', id: 2),
    FirstLevelCategory(name: '上装', id: 3),
    FirstLevelCategory(name: '下装', id: 4),
    FirstLevelCategory(name: '连衣裙', id: 5),
    FirstLevelCategory(name: '鞋子', id: 6),
    FirstLevelCategory(name: '包包', id: 7),
    FirstLevelCategory(name: '配饰', id: 8)
  ];
  var insertTestTwoLevelCategoryList2 = [
    TwoLevelCategory(name: '文胸', oid: 2),
    TwoLevelCategory(name: '三角裤', oid: 2),
    TwoLevelCategory(name: '睡衣', oid: 2),
    TwoLevelCategory(name: '保暖内衣', oid: 2),
  ];
  var insertTestTwoLevelCategoryList3 = [
    TwoLevelCategory(name: 'T恤', oid: 3),
    TwoLevelCategory(name: '衬衫', oid: 3),
    TwoLevelCategory(name: '毛衣', oid: 3),
    TwoLevelCategory(name: '外套', oid: 3),
    TwoLevelCategory(name: '夹克', oid: 3),
    TwoLevelCategory(name: '大衣', oid: 3),
    TwoLevelCategory(name: '西装', oid: 3)
  ];

  var insertTestTwoLevelCategoryList4 = [
    TwoLevelCategory(name: '牛仔裤', oid: 4),
    TwoLevelCategory(name: '裙子', oid: 4),
    TwoLevelCategory(name: '短裤', oid: 4),
    TwoLevelCategory(name: '运动裤', oid: 4),
    TwoLevelCategory(name: '西裤', oid: 4),
  ];
  var insertTestTwoLevelCategoryList5 = [
    TwoLevelCategory(name: '长款连衣裙', oid: 5),
    TwoLevelCategory(name: '短款连衣裙', oid: 5),
    TwoLevelCategory(name: '水平条纹连衣裙', oid: 5),
    TwoLevelCategory(name: '垂直条纹连衣裙', oid: 5),
  ];
  var insertTestTwoLevelCategoryList6 = [
    TwoLevelCategory(name: '运动鞋', oid: 6),
    TwoLevelCategory(name: '休闲鞋', oid: 6),
    TwoLevelCategory(name: '高跟鞋', oid: 6),
    TwoLevelCategory(name: '靴子', oid: 6),
    TwoLevelCategory(name: '凉鞋', oid: 6),
    TwoLevelCategory(name: '皮鞋', oid: 6),
  ];
  var insertTestTwoLevelCategoryList7 = [
    TwoLevelCategory(name: '手提包', oid: 7),
    TwoLevelCategory(name: '背包', oid: 7),
    TwoLevelCategory(name: '钱包', oid: 7),
    TwoLevelCategory(name: '单肩包', oid: 7),
    TwoLevelCategory(name: '斜挎包', oid: 7),
  ];

  var insertTestTwoLevelCategoryList8 = [
    TwoLevelCategory(name: '头饰', oid: 8),
    TwoLevelCategory(name: '项链', oid: 8),
    TwoLevelCategory(name: '手链', oid: 8),
    TwoLevelCategory(name: '耳环', oid: 8),
    TwoLevelCategory(name: '帽子', oid: 8),
    TwoLevelCategory(name: '眼镜', oid: 8),
  ];

  List<List<TwoLevelCategory>> _two = [];

  /// 一级分类添加默认数据
  Future<bool> insertTestOne() async {
    //事务处理
    return await _db.transaction<bool>((txn) async {
      //事务提交
      for (var element in insertTestFirstLevelCategoryList) {
        await txn.insert(firstLevelCategoryTableName, element.toJson());
      }
      return true;
    });
  }

  /// 二级分类添加默认 数据
  Future<void> insertTestTwo() async {
    _two.add(insertTestTwoLevelCategoryList2);
    _two.add(insertTestTwoLevelCategoryList3);
    _two.add(insertTestTwoLevelCategoryList4);
    _two.add(insertTestTwoLevelCategoryList5);
    _two.add(insertTestTwoLevelCategoryList6);
    _two.add(insertTestTwoLevelCategoryList7);
    _two.add(insertTestTwoLevelCategoryList8);
    var batch = _db.batch();
    //批处理
    for (var element in _two) {
      for (var element1 in element) {
        batch.insert(twoLevelCategoryTableName, element1.toJsonTest());
      }
    }
    await batch.commit();
  }

  /// 位置添加默认数据
  Future<void> insertTestLocation() async {}

  Future<void> close() async {
    await _db.close();
  }
}
